package com.hospital.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.hospital.dto.DignoseLogDto;
import com.hospital.dto.UserpaylogDto;
import com.hospital.entity.User;
import com.hospital.entity.Userlog;
import com.hospital.service.intf.UserlogService;
import com.hospital.util.SessionUtil;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author fxy
 * @date 2022/8/31
 */
@RestController
@RequestMapping("/api/userlog/")
public class UserlogController {
    @Autowired
    private UserlogService userlogService;

    //添加一条用户购买药品记录
    @GetMapping("/adduserlog.do")
    public R addUserMed(Userlog userlog){
        return userlogService.addUserlog(userlog);
    }

    //添加一条用户购买药品记录
    @GetMapping("/pageuserlog.do")
    public PageBean pagelog(int page, int limit, HttpServletRequest request){
        User seesion = SessionUtil.getSeesion(request);
        return userlogService.selectlog(page,limit,seesion.getId());
    }

    //查询,下载用户诊断记录
    @GetMapping("downpaylog.do")
    public void downdignoselog(HttpServletResponse response,HttpServletRequest request) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=dignose-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        User seesion = SessionUtil.getSeesion(request);
        List<UserpaylogDto> list= userlogService.selectlogall(seesion.getId());
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(), UserpaylogDto.class).sheet(System.currentTimeMillis()+"").doWrite(list);
    }

}
